package com.zt.config;

import com.zt.strategy.MsgHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 策略上下文
 *
 * @author zt
 * @date 2021/12/10 12:57
 */
@Component
public class MsgHandlerContext {
    @Autowired
    private ApplicationContext applicationContext;


    /**
     * 存放所有策略类Bean的map
     */
    public static Map<MsgType, Class<MsgHandler>> msgTypeClassMap= new HashMap<>();


    public MsgHandler getMsgHandler(MsgType msgType) {
        Class<MsgHandler> baseMsgHandlerClass = msgTypeClassMap.get(msgType);

        if(baseMsgHandlerClass==null) throw new IllegalArgumentException("没有类型");

        return applicationContext.getBean(baseMsgHandlerClass);
    }



}
